package com.iwop.moto.utils.http;
import com.alibaba.fastjson.JSON;
import com.iwop.moto.cache.RedisAPI;
import com.iwop.moto.model.User;
import com.iwop.moto.utils.stringUtil.JsonUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
 * Session 工具类
 * @author huyuangui
 * @time 2015-9-25 下午02:22:04
 */
public class SessionUtils {

	private static HttpServletRequest reqeust;

	public static HttpSession getSession(){
		return reqeust.getSession();
	}

	/** 将数据信息存入Redis缓存中 **/
	public static void setKeyValue(String key,Object value){
		//getSession().setAttribute(key, value);
		RedisAPI.put(key,JSON.toJSONString(value));
	}

	public static void setKeyValueString(String key,String value){
		//getSession().setAttribute(key, value);
		RedisAPI.put(key,value);
	}

	/** 获取Redis key对应的缓存信息 **/
	public static Object getValueFromSession(String key){
		//return getSession().getAttribute(key);
		return RedisAPI.get(key);
	}

	/** 获取Redis key对应的缓存信息 **/
	public static Object putValueFromSession(String key,Object value,int second){
		//return getSession().getAttribute(key);
		return RedisAPI.put(key,JSON.toJSONString(value),second);
	}

	/** 删除Redis key对应的缓存信息 **/
	public static void removeRedisByKey(String key){
		RedisAPI.del(key);
	}

	/**
	 * 获取当前登录用户 不管什么端
	 * @return
	 */
	public static User getSubjectUser(String session_id) {
		String  ujson = (String) SessionUtils.getValueFromSession(session_id);
		User user = JsonUtils.jsonToBean(ujson, User.class);
		return user;
	}
}
